package org.mspring.mlog.service;

import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.cache.TagCacheService;
import org.mspring.mlog.dao.TagEntityDao;
import org.mspring.mlog.entity.TagEntity;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-02 15:51:24
 */
@Service
public class TagEntityService {

    private static final Logger LOG = LoggerFactory.getLogger(TagEntityService.class);

    private TagEntityDao tagEntityDao;
    private TagCacheService tagCacheService;

    public TagEntity findById(long id) {
        return tagEntityDao.findById(id);
    }

    public PageResult<TagEntity> list(List<SqlCondition> conditions, SqlPager pager) {
        return tagEntityDao.listPage(conditions, pager);
    }

    /**
     * 根据名称模糊查找
     *
     * @param name
     * @return
     */
    public List<TagEntity> listByName(String name) {
        return tagEntityDao.listByName(name);
    }

    /**
     * 根据名称查找
     *
     * @param name
     * @return
     */
    public TagEntity findByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return tagEntityDao.findByName(name);
    }

    public List<TagEntity> listAll() {
        return tagEntityDao.listAll();
    }

    @Transactional
    public boolean insert(TagEntity tag) {
        return tagEntityDao.insert(tag);
    }

    @Transactional
    public boolean insertOrUpdate(TagEntity tag) {
        if (tag.getId() == null || tag.getId() <= 0) {
            return tagEntityDao.insert(tag);
        } else {
            return tagEntityDao.update(tag);
        }
    }

    @Transactional
    public boolean update(TagEntity tag) {
        return tagEntityDao.update(tag);
    }

    @Transactional
    public boolean deleteById(long id) {
        return tagEntityDao.deleteById(id);
    }

    @Autowired
    public void setTagEntityDao(TagEntityDao tagEntityDao) {
        this.tagEntityDao = tagEntityDao;
    }

    @Autowired
    public void setTagCacheService(TagCacheService tagCacheService) {
        this.tagCacheService = tagCacheService;
    }
}